import pymysql


def main():
    conn = pymysql.connect(host='8.147.131.121', port=3306,
                           user='root1',password='123456',
                           db='school', charset='utf8')
    try:
        with conn.cursor() as cursor:
            result = cursor.execute(
                'insert into tb_college values(4,"商学院","商小才","http://www.asa.com")')  # ''字符串里写SQL语句
            if result == 1:
                print('数据插入成功！')
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
        conn.rollback()  # 不成功就 回滚
    finally:
        conn.close()  # 保证这段代码一定会执行
    print(conn)


if __name__ == '__main__':
    main()
